home *** CD-ROM | disk | FTP | other *** search
/ Aminet 37 / Aminet 37 (2000)(Schatztruhe)[!][Jun 2000].iso / Tools / ClassAct / Examples / ARexx / ARexxExample.c < prev    next >
C/C++ Source or Header  |  1997-04-25  |  6KB  |  293 lines

  1. #include <dos/dos.h>
  2. #include <dos/datetime.h>
  3. #include <libraries/gadtools.h>
  4.  
  5. #include <clib/alib_protos.h>
  6.  
  7. #include <proto/exec.h>
  8. #include <proto/intuition.h>
  9. #include <proto/dos.h>
  10. #include <proto/utility.h>
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15.  
  16. #include <classact.h>
  17. #include <classact_macros.h>
  18.  
  19. #ifdef _DCC
  20. #define SAVEDS __geta4
  21. #define ASM
  22. #define REG_A0 __A0
  23. #define REG_A1 __A1
  24. #else
  25. #define SAVEDS __saveds
  26. #define ASM __asm
  27. #define REG_A0 register __a0
  28. #define REG_A1 register __a1
  29. #endif
  30.  
  31. #ifdef _DCC
  32. extern __stkargs ULONG HookEntry();
  33. #else
  34. extern __stdargs ULONG HookEntry();
  35. #endif
  36.  
  37. /* Gadget IDs.
  38.  */
  39. #define GAD_QUIT 1
  40.  
  41. /* ARexx command IDs.
  42.  */
  43. enum { REXX_NAME, REXX_VERSION, REXX_AUTHOR, REXX_SEND, REXX_DATE };
  44.  
  45.  
  46. /* Protos for the reply hook and ARexx command functions.
  47.  */
  48. VOID SAVEDS reply_callback(struct Hook *, Object *, struct RexxMsg *);
  49. VOID ASM rexx_Name(REG_A0 struct ARexxCmd *, REG_A1 struct RexxMsg *);
  50. VOID ASM rexx_Version(REG_A0 struct ARexxCmd *, REG_A1 struct RexxMsg *);
  51. VOID ASM rexx_Author(REG_A0 struct ARexxCmd *, REG_A1 struct RexxMsg *);
  52. VOID ASM rexx_Send(REG_A0 struct ARexxCmd *, REG_A1 struct RexxMsg *);
  53. VOID ASM rexx_Date(REG_A0 struct ARexxCmd *, REG_A1 struct RexxMsg *);
  54.  
  55. /* Buffer for the system date.
  56.  */
  57. UBYTE systemDate[32];
  58.  
  59. /* Our reply hook function.
  60.  */
  61. struct Hook reply_hook;
  62.  
  63. /* The following commands are valid for this demo.
  64.  */
  65. struct ARexxCmd Commands[] =
  66. {
  67.     { "NAME",         REXX_NAME,        rexx_Name,        NULL,        NULL, },
  68.     { "VERSION",      REXX_VERSION,    rexx_Version,    NULL,        NULL, },
  69.     { "AUTHOR",       REXX_AUTHOR,    rexx_Author,    NULL,        NULL, },
  70.     { "SEND",       REXX_SEND,        rexx_Send,        "TEXT/F",    NULL, },
  71.     { "DATE",         REXX_DATE,        rexx_Date,         "SYSTEM/S",    NULL, },
  72.     { NULL,            NULL,            NULL,            NULL,        NULL, }
  73. };
  74.  
  75.  
  76. /* Starting point.
  77.  */
  78. int main(int argc, char *argv[])
  79. {
  80.     Object *arexx_obj;
  81.  
  82.     if (!ButtonBase) return(20);
  83.  
  84.     /* Create host object.
  85.      */
  86.     if (arexx_obj = ARexxObject,
  87.                         AREXX_HostName, "AREXXDEMO",
  88.                         AREXX_Commands, Commands,
  89.                         AREXX_NoSlot, TRUE,
  90.                         AREXX_ReplyHook, &reply_hook,
  91.                         End)
  92.     {
  93.         Object *win_obj;
  94.  
  95.         /* Create the window object.
  96.          */
  97.         if (win_obj = WindowObject,
  98.                         WA_Title, "ClassAct arexx.class Demo",
  99.                         WA_DragBar, TRUE,
  100.                         WA_CloseGadget, TRUE,
  101.                         WA_DepthGadget, TRUE,
  102.                         WINDOW_ParentGroup, LayoutObject,
  103.                             LAYOUT_AddChild, ButtonObject,
  104.                                 GA_Text, "_Quit",
  105.                                 GA_ID, GAD_QUIT,
  106.                                 GA_RelVerify, TRUE,
  107.                                 ButtonEnd,
  108.                             LayoutEnd,
  109.                         EndWindow)
  110.         {
  111.             struct Window *window;
  112.  
  113.             /* try to open the window.
  114.              */
  115.             if (window = (struct Window *)CA_OpenWindow(win_obj))
  116.             {
  117.                 ULONG wnsig = 0, rxsig = 0, signal, result, Code;
  118.                 BOOL running = TRUE;
  119.  
  120.                 /* Setup the reply callback hook.
  121.                  */
  122.                 reply_hook.h_Entry = HookEntry;
  123.                 reply_hook.h_SubEntry = reply_callback;
  124.                 reply_hook.h_Data = NULL;
  125.  
  126.                 /* Try to start the macro "Demo.rexx".  Note that the
  127.                  * current directory and REXX: will be searched for this
  128.                  * macro.  Our reply hook will get the results of our
  129.                  * efforts to start this macro.  To be totally robust, we
  130.                  * should have also passed pointers for the various result
  131.                  * variables.
  132.                  */
  133.                 DoMethod(arexx_obj, AM_EXECUTE, "Demo.rexx", NULL, NULL, NULL, NULL, NULL);
  134.  
  135.                 /* Obtain wait masks.
  136.                  */
  137.                 GetAttr(WINDOW_SigMask, win_obj, &wnsig);
  138.                 GetAttr(AREXX_SigMask, arexx_obj, &rxsig);
  139.  
  140.                 /* Event loop...
  141.                  */
  142.                 do
  143.                 {
  144.                     signal = Wait(wnsig | rxsig | SIGBREAKF_CTRL_C);
  145.  
  146.                     /* ARexx event?
  147.                      */
  148.                     if (signal & rxsig)
  149.                         CA_HandleRexx(arexx_obj);
  150.  
  151.                     /* Window event?
  152.                      */
  153.                     if (signal & wnsig)
  154.                     {
  155.                         while ((result = CA_HandleInput(win_obj, &Code)) != WMHI_LASTMSG)
  156.                         {
  157.                             switch (result & WMHI_CLASSMASK)
  158.                             {
  159.                                 case WMHI_CLOSEWINDOW:
  160.                                     running = FALSE;
  161.                                     break;
  162.  
  163.                                 case WMHI_GADGETUP:
  164.                                     switch(result & WMHI_GADGETMASK)
  165.                                     {
  166.                                         case GAD_QUIT:
  167.                                             running = FALSE;
  168.                                             break;
  169.                                     }
  170.                                     break;
  171.  
  172.                                 default:
  173.                                     break;
  174.                             }
  175.                         }
  176.                     }
  177.  
  178.                     if (signal & SIGBREAKF_CTRL_C)
  179.                     {
  180.                         running = FALSE;
  181.                     }
  182.                 }
  183.                 while (running);
  184.             }
  185.             else
  186.                 puts ("Could not open the window");
  187.             DisposeObject(win_obj);
  188.         }
  189.         else
  190.             puts("Could not create the window object");
  191.         DisposeObject(arexx_obj);
  192.     }
  193.     else
  194.         puts("Could not create the ARexx host.");
  195.  
  196.     return(0);
  197. }
  198.  
  199. #ifdef _DCC
  200. int wbmain(struct WBStartup *wbs)
  201. {
  202.     return(main(0, NULL));
  203. }
  204. #endif
  205.  
  206.  
  207. /* Note the use of SAVEDS, it is required for the callback
  208.  * ARexx command functions if access the global data such as
  209.  * systemData[] made in the callback.
  210.  */
  211.  
  212. /* This function gets called whenever we get an ARexx reply.  In this example,
  213.  * we will see a reply come back from the REXX server when it has finished
  214.  * attempting to start the Demo.rexx macro.
  215.  */
  216. VOID SAVEDS reply_callback(struct Hook *hook, Object *o, struct RexxMsg *rxm)
  217. {
  218.     Printf("Args[0]: %s\nResult1: %ld  Result2: %ld\n",
  219.         rxm->rm_Args[0], rxm->rm_Result1, rxm->rm_Result2);
  220. }
  221.  
  222. /* NAME
  223.  */
  224. VOID SAVEDS ASM rexx_Name(REG_A0 struct ARexxCmd *ac, REG_A1 struct RexxMsg *rxm)
  225. {
  226.     /* return the program name.
  227.      */
  228.     ac->ac_Result = "ARexxTest";
  229. }
  230.  
  231. /* VERSION
  232.  */
  233. VOID SAVEDS ASM rexx_Version(REG_A0 struct ARexxCmd *ac, REG_A1 struct RexxMsg *rxm)
  234. {
  235.     /* return the program version.
  236.      */
  237.     ac->ac_Result = "1.0";
  238. }
  239.  
  240. /* AUTHOR
  241.  */
  242. VOID SAVEDS ASM rexx_Author(REG_A0 struct ARexxCmd *ac, REG_A1 struct RexxMsg *rxm)
  243. {
  244.     /* return the authors name.
  245.      */
  246.     ac->ac_Result = "Phantom Development LLC";
  247. }
  248.  
  249. /* SEND
  250.  */
  251. VOID SAVEDS ASM rexx_Send(REG_A0 struct ARexxCmd *ac, REG_A1 struct RexxMsg *rxm)
  252. {
  253.     /* Print some text
  254.      */
  255.     if (ac->ac_ArgList[0])
  256.         Printf("%s\n", (STRPTR)ac->ac_ArgList[0]);
  257. }
  258.  
  259. /* DATE
  260.  */
  261. VOID SAVEDS ASM rexx_Date(REG_A0 struct ARexxCmd *ac, REG_A1 struct RexxMsg *rxm)
  262. {
  263.     struct DateTime dt;
  264.  
  265.     /* SYSTEM switch specified?
  266.      */
  267.     if (!ac->ac_ArgList[0])
  268.     {
  269.         /* return the compilation date.
  270.          */
  271.         ac->ac_Result = "11-10-95";
  272.     }
  273.     else
  274.     {
  275.         /* compute system date and store in systemDate buffer
  276.          */
  277.         DateStamp((struct DateStamp *)&dt);
  278.  
  279.         dt.dat_Format  = FORMAT_USA;
  280.         dt.dat_Flags   = 0;
  281.         dt.dat_StrDay  = NULL;
  282.         dt.dat_StrDate = systemDate;
  283.         dt.dat_StrTime = NULL;
  284.  
  285.         DateToStr(&dt);
  286.  
  287.         /* return system date
  288.          */
  289.         ac->ac_Result = systemDate;
  290.     }
  291. }
  292.  
  293.